package ner;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import ner.POStagger.TaggingResult;
import com.aliasi.util.Files;

public class InformationExtractor {

	
	private Segmenter segmenter;
	private POStagger posTagger;
	private NERecognizer neRecognizer;
	private CharacterDetailsExtractor characterDetailsCollector;
	private DeclinationBasicRecognizer declinationBasicRecognizer;
	
	private boolean basicMode=false;
	
	InformationExtractor(){
		
		segmenter=new Segmenter();
		posTagger=new POStagger();
		neRecognizer=new NERecognizer();
		characterDetailsCollector=new CharacterDetailsExtractor();
		declinationBasicRecognizer=new DeclinationBasicRecognizer();
		
	}
	
	public void extractInfo(ExtractionTask extractionTask) {
		
		try {
			
			String content = Files.readFromFile(new File(extractionTask.getInputFilename()),"ISO-8859-2");//TODO encoding from args
			extractionTask.setInputContent(content);
			splitFindBaseWordsAndTagPOS(extractionTask);
			Set<BookCharacter> characters=neRecognizer.performInitialNER(extractionTask,segmenter);
			extractionTask.setCharacters(characters);
			characterDetailsCollector.setGenderAndNumber(extractionTask);
			characterDetailsCollector.findSynonyms(extractionTask);
		//	declinationBasicRecognizer.recognizeDeclination(extractionTask);
			characterDetailsCollector.findPositivesNegativesAndDescs(extractionTask);
			characterDetailsCollector.assessBookCharacters(extractionTask);
			
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}		

	}

	private void splitFindBaseWordsAndTagPOS(ExtractionTask extractionTask) throws UnsupportedEncodingException {
	
		
		List<String> sentences=segmenter.splitBySentences(extractionTask.getInputContent());
		extractionTask.setSentences(sentences);	
		List<List<String>> POStags=new LinkedList<List<String>>();
		List<List<String>> baseWords=new LinkedList<List<String>>();
		
		
		for(String s:sentences){
			
			TaggingResult taggingResult=posTagger.recognizePOS(segmenter.splitByWords(s));
			POStags.add(taggingResult.getPOStags());
			baseWords.add(taggingResult.getBaseWords());
			
		}
		//for(String s:suspected){
		//	System.out.println(s);
		//}
		
		extractionTask.setPOStags(POStags);
		extractionTask.setBaseWords(baseWords);
		
	}

	
	

}
